In [1]:
%matplotlib inline
from ggplot import *

Making a Histogram

To make a histogram in ggplot you have 2 options: geom_histogram() or qplot. qplot is a short-hand version of geom_histogram, so we'll start with qplot and then work towards a more complex example.

qplot

The easiest way to make a histogram is to pass data to the qplot function. By default if you give qplot only 1 variable, it will plot a histogram of that variable.


In [2]:
qplot(diamonds.price)


Out[2]:
<ggplot: (273924449)>

qplot can also plot numpy arrays and lists. To do this, just pass in the array or list to the qplot function as you would with a pandas Series.


In [3]:
import numpy as np
x = np.random.normal(0, 1, 1000)
qplot(x)


Out[3]:
<ggplot: (274115921)>

geom_histogram

geom_histogram requires slightly more code, but it gives you more control over how your plots will look. In the example below, I'm going to make the same price histogram as I did above, but I'll use a ggplot base layer and add a geom_histogram to it.


In [4]:
ggplot(diamonds, aes(x='price')) + geom_histogram()


Out[4]:
<ggplot: (285317665)>

geom_histogram makes it easier to control other aesthetics of your plot. For example if you wanted the bars to be different colors based on the cut of the diamond, you could do that by adding a fill='cut' aesthetic to your base layer.


In [5]:
ggplot(diamonds, aes(x='price', fill='cut')) + geom_histogram()


Out[5]:
<ggplot: (284826789)>

In [ ]: